package com.weitu.permission.userRole.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.permission.user.entity.User;
import com.weitu.permission.user.entity.UserRole;
import com.weitu.permission.user.service.UserService;
import com.weitu.permission.userRole.service.UserRoleService;
import com.weitu.util.AjaxResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiMeng 2017/3/20
 */
@Controller
@RequestMapping(value = "permission/userRole")
public class UserRoleController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/userRole";
    private final UserService userService;
    private final UserRoleService userRoleService;

    @Autowired
    public UserRoleController(UserService userService, UserRoleService userRoleService) {
        this.userService = userService;
        this.userRoleService = userRoleService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, String id) {
        User user = userService.selectById(id);

        model.addAttribute(user);
        return MODULE_BATH_PATH + "/edit";
    }

    @SystemLog(module = "用户角色维护", methods = "授予角色")
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxResult editAjax() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        String id = paramMap.get("id").toString();
        String newRoleWithDept = paramMap.get("newRoleWithDept").toString();

        if (StringUtils.isEmpty(newRoleWithDept)){
            if (userRoleService.deleteUserRoleByUserId(id)){
                result.setMessage("角色授予成功！");
                result.setSuccess(true);
                return result;
            }
            else {
                result.setMessage("角色授予失败！");
                return result;
            }
        }

        List<UserRole> roleWithDeptList = getRoleWithDeptList(id, newRoleWithDept);
        if (userRoleService.updateUserRole(id, roleWithDeptList)){
            result.setSuccess(true);
            result.setMessage("角色授予成功！");
            return result;
        }else {
            result.setMessage("角色授予失败！");
            return result;
        }
    }

    /**
     * 处理字符串参数为用户角色实体列表
     * @param id 用户id
     * @param newRoleWithDept 要插入的角色和部门id，字符串格式："部门1id-角色id,部门2id-角色id"
     * @return 用户角色实体列表
     */
    private List<UserRole> getRoleWithDeptList(String id, String newRoleWithDept) {
        List<UserRole> newRoleWithDeptList = new ArrayList<UserRole>();

        String[] roleWithDeptAllString = newRoleWithDept.split(",");
        for (String RoleWithDeptAllString : roleWithDeptAllString) {
            String deptId = RoleWithDeptAllString.split("-")[0];
            String roleId = RoleWithDeptAllString.split("-")[1];
            UserRole userRole = new UserRole();
            userRole.setUserId(id);
            userRole.setDeptId(deptId);
            userRole.setRoleId(roleId);
            newRoleWithDeptList.add(userRole);
        }
        return newRoleWithDeptList;
    }

}
